package com.rlf.sf.legacymapping.xml;

import java.util.List;
import java.util.stream.Collectors;

import com.rlf.sf.legacymapping.fileparse.FileParse;
import com.rlf.sf.legacymapping.vo.LegacyCategoryVo;
import com.rlf.sf.legacymapping.vo.PermissionVo;
import com.rlf.sf.legacymapping.vo.SectionVo;
import com.rlf.sf.legacymapping.xml.object.LegacyCategory;
import com.rlf.sf.legacymapping.xml.object.LegacyPermission;
import com.rlf.sf.legacymapping.xml.object.RbpPermission;
import com.rlf.sf.legacymapping.xml.object.RootLegacyPermissionMapping;
import com.rlf.sf.legacymapping.xml.object.Section;

public class LegacyPermissionMappingXMLGenerator {
	public static void main(String[] args) {
		List<SectionVo> sections = FileParse.getParseFileData("src/main/resource");
		RootLegacyPermissionMapping root = convertSectionsToRootObject(sections);
		System.out.println(XmlUtil.toXmlMessageWithTag(root));
	}
	
	public static RootLegacyPermissionMapping convertSectionsToRootObject (List<SectionVo> sections) {
		RootLegacyPermissionMapping root = new RootLegacyPermissionMapping();
		
		for (SectionVo sectionVo : sections) {
			Section section = new Section ();
			section.setMessageKey(sectionVo.getMessageKey());
			
			String modulePath = sectionVo.getModule();
			String moduleName1 = modulePath.substring(modulePath.lastIndexOf("\\"), modulePath.length()-1);
			String moduleName = moduleName1.substring(0, moduleName1.indexOf("_"));
			
			for (LegacyCategoryVo categoryVo: sectionVo.getLegacyPermissionVos()) {
				LegacyCategory category = new LegacyCategory();
				category.setMessageKey(categoryVo.getLegecyCategroyMessageKey());
				
				for (PermissionVo permissionVo: categoryVo.getPermissionVos()) {
					
					LegacyPermission legacyPermission = new LegacyPermission();
					legacyPermission.setMessageKey(permissionVo.getLegecyPermissionMessageKey());
					legacyPermission.setPermissionType(permissionVo.getLegecyPermissionType());
					legacyPermission.setPermissionStringValue(permissionVo.getLegecyPermissionStringValue());
					
					RbpPermission rbpPermission = new RbpPermission();
					rbpPermission.setMessageKey(permissionVo.getRbpPermissionMessageKey());
					rbpPermission.setPermissionType(permissionVo.getRbpPermissionType());
					rbpPermission.setPermissionStringValue(permissionVo.getRbpPermissionStringValue());
					
					legacyPermission.setRbpPermission(rbpPermission);
					
					//For mappings checking -- duplicated permission mappings:
					if(category.getLegacyPermissions().contains(legacyPermission)) {
						System.out.println("Duplicated permission mapping: " + String.format("%s-%s-%s-%s", moduleName, legacyPermission.getPermissionType(), legacyPermission.getPermissionStringValue(), legacyPermission.getMessageKey()));
					}
					
					//For mappings checking -- Different multiple legacy permission mapping to same one RBP permission
					List<LegacyPermission> multiLegacy = category
							.getLegacyPermissions()//
							.stream()//
							.filter(legacy -> legacy.getRbpPermission().equals(rbpPermission))//
							.collect(Collectors.toList());
					
					if(multiLegacy.size() > 0) {
						System.out.println("Different multiple legacy permission mapping to same one RBP permission： ");
						System.out.println(multiLegacy.toString());
					}
					
					//Ignore repeated legacy permissions under the same category
					if(!category.getLegacyPermissions().contains(legacyPermission)) {
						category.getLegacyPermissions().add(legacyPermission);
					} 
				}
				
				section.getCategories().add(category);
			}
			root.getSections().add(section);
		}
		
		return root;
	}
	
}
